<?php

class Blog extends Controller {

	function Blog()
	{
		parent::Controller();
	}
	
  /**
   * Blog homepage
   */
	function index()
	{
	  $this->load->model('blog_model');
    $data['latestPostList_initial'] = $this->blog_model->select_latestPostList(10, 0);
    
    // reformalize the post arrays
    $i = 0;
    foreach($data['latestPostList_initial'] as $latestPostList_item)
    {
      $data['latestPostList'][$i] = $latestPostList_item;
      $postId = $data['latestPostList'][$i]['id'];
      
      // count post comments
      $data['latestPostList'][$i]['comments'] = $this->blog_model->count_postComments($postId);

      // determine read more content
      $readmore = $this->blog_model->select_postContentMore($postId);
      
      if($readmore) $data['latestPostList'][$i]['more'] = 1;
      else $data['latestPostList'][$i]['more'] = 0;
      $i++;
    }
    
    $data['pageTitle'] = "Blog";
    $data['bodyId'] = "secBlog";
		$this->load->view('blog_view', $data);
	}
	
  /*
   * Display the post with all its comments
   */
	function display_post()
	{
	  $this->load->model('blog_model');
	  $permalink = substr($this->uri->segment(2), 0, -7);
	  
	  $data['postData'] = $this->blog_model->select_postWithComments($permalink);
	  
    $data['pageTitle'] = $data['postData']['title'];
    $data['bodyId'] = "secBlog";
	  $this->load->view('blog_display_post', $data);
	}
}
?>